using System;
using Impact.Model.Objects;
using SubSonic;

namespace Impact.Model.Service
{
    public class EventService : IEventService
    {

        #region IModelService<Event,EventCollection> Members

        public EventCollection GetAll( bool onlyEnabled )
        {
            EventCollection output = new EventCollection();
            if ( onlyEnabled )
            {
                output.Where( Event.Columns.Enabled, true );
            }
            return output.Load();
        }

        public Event Get( string id )
        {
            return Event.FetchByID( id );
        }

        public void Delete( string id )
        {
            Event.Delete( id );
        }

        public void Save( Event evt, Guid userId )
        {
            evt.Save( userId );
        }

        #endregion

        #region IEventService Members

        public EventCollection GetFutureEvents( int count )
        {
            Query q = Event.CreateQuery();
            q.WHERE( Event.Columns.StartTime, SubSonic.Comparison.GreaterOrEquals, DateTime.Now.AddDays( -1 ) );
            q.OrderBy = OrderBy.Asc( Event.Columns.StartTime );
            q.Top = count.ToString();

            EventCollection output = new EventCollection();
            output.LoadAndCloseReader( q.ExecuteReader() );

            return output;
        }

        #endregion
    }
}
